/*
pthread_mutex_init(&mutex,nullptr)
pthread_mutex_lock(&mutex)
pthread_mitex_trylock(&mutex)
pthread_mutex_unlock(&mutex)
pthread_mutex_destroy(&mutex)
*/


#include <iostream>
#include <pthread.h>


using namespace std;


pthread_mutex_t mutex;
int counter = 0;
int thread_retval = 1;

void* increment(void *arg)
{
	pthread_mutex_lock(&mutex);
	counter++;
	pthread_mutex_unlock(&mutex);

	pthread_exit(&thread_retval);
}


int main(int argc,char** argv)
{
	int *thread_exitval;
	pthread_t t1,t2;
	pthread_mutex_init(&mutex,nullptr);

	pthread_create(&t1,nullptr,increment,nullptr);
	pthread_create(&t2,nullptr,increment,nullptr);
	
	pthread_join(t1,nullptr);
	pthread_join(t2,(void**)&thread_exitval);

	pthread_mutex_destroy(&mutex);

	cout << "retval:" << *thread_exitval << endl;
	cout << "counter:" << counter << endl;

	return 0;
}



